// Copyright (c) 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
*@describe:
*@author wfl19/Kristas
*@date 2021/12/18
 */

package tk

import (
	"gitee.com/kristas/booting-go/framework/core/statement/types"
	"gitee.com/kristas/booting-go/framework/data/datasource"
	"gitee.com/kristas/booting-go/framework/data/tk/select_mapper"
	"gitee.com/kristas/booting-go/plugin/gorm_plugin"
	"gorm.io/gorm"
)

func NewMapper(datasource, table string) *Mapper {
	return &Mapper{table: table, datasource: datasource}
}

type Mapper struct {
	types.Component
	table      string
	datasource string
	db         *gorm.DB
	*select_mapper.SelectMapper
	*select_mapper.SelectByPrimaryKeyMapper
	*select_mapper.SelectAllMapper
	*select_mapper.SelectExampleMapper
}

func (a *Mapper) Bean() string {
	return "tk_mapper:" + a.datasource + "@" + a.table
}

func (a *Mapper) DataSource() string {
	return a.datasource
}

func (a *Mapper) SetDB(db datasource.DB) {
	gormDB := gorm_plugin.MustBindDB(db)
	a.SelectMapper = select_mapper.NewSelectMapper(a.table, gormDB)
	a.SelectByPrimaryKeyMapper = select_mapper.NewSelectByPrimaryKeyMapper(a.table, gormDB)
	a.SelectAllMapper = select_mapper.NewSelectAllMapper(a.table, gormDB)
	a.SelectExampleMapper = select_mapper.NewSelectExampleMapper(a.table, gormDB)
}
